/* 
 * Spinneret - Semi-Structured Database Engine
 * Copyright (c) 2009 Tom Bradford <spinneretdb.org>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

#include <signal.h>
#include <stdio.h>

#include "utils/list.h"
#include "utils/signal.h"
#include "utils/table.h"

#define BUF_SIZE 64

/* Private Types and Variables ***********************************************/

static sdb_table_t* signals;

/** Implementation type for Signal Handler instances */
struct sdb_signal_t {
    int signalNumber;
    sdb_signal_function_t* handlerFunction;
    void* context;
};

/* Private Functions *********************************************************/

static sdb_table_t* private_getSignals(void)
{
    if ( !signals ) {
        sdb_mem_t* mem = sdb_mem_new(NULL);
        signals = sdb_table_new(mem);
    }
        
    return signals;
}

/*---------------------------------------------------------------------------*/

static sdb_list_t* private_getHandlers(int signalNumber)
{
    sdb_table_t* signals = private_getSignals();
    sdb_mem_t* mem = sdb_ptr_getMem(signals);   
    
    char* str = sdb_mem_malloc(mem, BUF_SIZE);
    snprintf(str, BUF_SIZE, "%d", signalNumber);
    
    sdb_list_t* handlers = sdb_table_getItem(signals, str);
    
    if ( !handlers ) {
        handlers = sdb_list_new(mem);
        
        sdb_table_setItem(signals, str, handlers);
    }
    
    return handlers;
}

/*---------------------------------------------------------------------------*/

static void private_dispatchSignal(sdb_list_t* list, sdb_signal_t* handler, 
                                   void* context)
{
    if ( handler->handlerFunction )
        handler->handlerFunction(handler, handler->context);
}

/*---------------------------------------------------------------------------*/

static void private_signalHandler(int signalNumber)
{    
    sdb_list_t* handlers = private_getHandlers(signalNumber);
    sdb_list_iterate(handlers, private_dispatchSignal, NULL);
    
    if ( sdb_list_isEmpty(handlers) ) 
        signal(signalNumber, SIG_DFL); // Restore the default
    else                 
        signal(signalNumber, private_signalHandler);        
}

/* Public Functions **********************************************************/

sdb_signal_t* sdb_signal_new(int signalNumber, 
                                sdb_signal_function_t* handlerFunction,
                                void* context)
{
    sdb_table_t* signals = private_getSignals();
    sdb_mem_t* mem = sdb_ptr_getMem(signals);   
    
    sdb_signal_t* handler = sdb_mem_malloc(mem, sizeof(sdb_signal_t));
    
    handler->signalNumber = signalNumber;
    handler->handlerFunction = handlerFunction;
    handler->context = context;

    sdb_list_t* handlers = private_getHandlers(handler->signalNumber);
    bool wasEmpty = sdb_list_isEmpty(handlers);

    // Add the handler to the handlers set
    sdb_list_addItem(handlers, handler);

    // Register the handler for the specified signal number if necessary
    if ( wasEmpty )
        signal(signalNumber, private_signalHandler);

    return handler;    
}

/*---------------------------------------------------------------------------*/

int sdb_signal_getSignalNumber(sdb_signal_t* handler)
{
    return handler->signalNumber;
}

/*---------------------------------------------------------------------------*/

void sdb_signal_setHandlerFunction(sdb_signal_t* handler, 
                                   sdb_signal_function_t* handlerFunction)
{
    handler->handlerFunction = handlerFunction;
}

/*---------------------------------------------------------------------------*/

sdb_signal_function_t* sdb_signal_getHandlerFunction(sdb_signal_t* handler)
{
    return handler->handlerFunction;
}

/*---------------------------------------------------------------------------*/

void sdb_signal_setContext(sdb_signal_t* handler, void* context)
{
    handler->context = context;
}

/*---------------------------------------------------------------------------*/

void* sdb_signal_getContext(sdb_signal_t* handler)
{
    return handler->context;
}

/*---------------------------------------------------------------------------*/

void sdb_signal_remove(sdb_signal_t* handler)
{
    sdb_list_t* handlers = private_getHandlers(handler->signalNumber);
    sdb_list_removeItem(handlers, handler);
    
    if ( sdb_list_isEmpty(handlers) )
        signal(handler->signalNumber, SIG_DFL);
}
